package com.example.novelweb.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.novelweb.common.BaseResponse;
import com.example.novelweb.common.ErrorCode;
import com.example.novelweb.exception.BusinessException;
import com.example.novelweb.exception.ThrowUtils;
import com.example.novelweb.model.dto.comment.CommentAddRequest;
import com.example.novelweb.model.dto.comment.CommentQueryRequest;
import com.example.novelweb.model.vo.CommentVO;
import com.example.novelweb.service.CommentService;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/comment")
public class CommentController {

  @Resource
  CommentService commentService;


  @GetMapping("/page/novel")
  public BaseResponse<Page<CommentVO>>  pageNovelComment(CommentQueryRequest commentQueryRequest, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(commentQueryRequest == null , ErrorCode.PARAMS_ERROR,"null params");
    Long novelId = commentQueryRequest.getNovelId();
    ThrowUtils.throwIf(novelId ==null || novelId<=0, ErrorCode.PARAMS_ERROR);

    return commentService.pageCommentByNovelId(commentQueryRequest);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/add")
  public BaseResponse<Long> addComment(@RequestBody CommentAddRequest commentAddRequest, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(commentAddRequest == null ,ErrorCode.PARAMS_ERROR);
    ThrowUtils.throwIfCheckIdsAny(
        commentAddRequest.getNovelId()
    );
    Long replierId = commentAddRequest.getReplierId();
    Long parentId = commentAddRequest.getParentId();
    Long topId = commentAddRequest.getTopId();
    ThrowUtils.throwIf(replierId==null || replierId<0, ErrorCode.PARAMS_ERROR,"null or invalid param id");
    ThrowUtils.throwIf(parentId==null || parentId<0, ErrorCode.PARAMS_ERROR,"null or invalid param id");
    ThrowUtils.throwIf(topId==null || topId<0, ErrorCode.PARAMS_ERROR,"null or invalid param id");

    ThrowUtils.throwIf(StringUtils.isBlank(commentAddRequest.getContent()),ErrorCode.PARAMS_ERROR,"评论内容为空");
    return commentService.add(commentAddRequest);

  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/del")
  public BaseResponse<Boolean> delComment(@RequestParam("commentId") Long commentId,HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(commentId == null || commentId<=0 ,ErrorCode.PARAMS_ERROR);
    return commentService.del(commentId);
  }


}
